#include <bits/stdc++.h>
#define ll long long
#define P vector<pair<ll,ll> >
using namespace std;const ll A = 2e5 + 5;
ll rd(){char c = getchar();ll f = 1,x = 0;
	while(!isdigit(c)){if(c == '-')f = -1;c = getchar();}
	while(isdigit(c))x = x * 10 + c - '0',c = getchar();
	return x * f;}
ll t,n,m,_a[A],a[A],b[A],w,_t[4],s[A],S,_s;bool f[20][A];char c;
P ans[20];
P dfs(ll l,ll r){P g;ll mid = l + r >> 1;
	if(l == r){g.push_back({l,0});return g;}
	P L = dfs(l,mid),R = dfs(mid + 1,r);
	ll p = __lg(r - l + 1),q = (l >> p) + 1;
	bool o = 0;// other
	if(f[p][q]){// you
		for(auto i : R)if(i.second >= p)g.push_back(i);
			else{pair<ll,ll> x = i;x.second = p,o = 1,g.push_back(x);}
		if(o)for(auto i : L)g.push_back(i);
	}else{// zuo
		for(auto i : L)if(i.second >= p)g.push_back(i);
			else{pair<ll,ll> x = i;x.second = p,o = 1,g.push_back(x);}
		if(o)for(auto i : R)g.push_back(i);
	}return g;
}
int main(){
	freopen("arena.in","r",stdin);
//	freopen("arena.out","w",stdout);
	n = rd(),m = rd();
	for(ll i = 1;i <= n;i++)_a[i] = rd();
	for(ll i = 1;i <= n;i++)b[i] = rd();
	w = __lg(n - 1) + 1;
	for(ll i = 1;(1ll << i - 1) <= n;i++){
		c = getchar();while(!isdigit(c))c = getchar();
		for(ll j = 1;j <= (1ll << w - i);j++)
			f[i][j] = (c == '1'),c = getchar();
	}for(ll i = 0;i <= w;i++)ans[i] = dfs(1,(1ll << i)),
		sort(ans[i].begin(),ans[i].end());
//	for(auto i : ans[w])cout << i.first << " " << i.second << '\n';
	t = rd();while(t--){S = 0;for(ll i = 0;i < 4;i++)_t[i] = rd();
		for(ll i = 1;i <= n;i++)a[i] = (_a[i] ^ _t[i % 4]);
		memset(s,-1,sizeof s);
		for(ll i = n + 1;i <= (1ll << w);i++)a[i] = 1e18;
	//	for(ll i = 1;i <= n;i++)cout << a[i] << ' ';cout << '\n';
		for(ll W = w;W >= 1;W--){_s = 0;
			stack<ll> st;for(auto i : ans[W])_s += i.first;s[0] = _s;
			for(ll i = 0;i < ans[W].size();i++){
				ll now = ans[W][i].first;bool ok = 1;
				if(a[now] < ans[W][i].second)ok = 0;
				while(st.size()){ll x = st.top();
					ll p = __lg(now ^ x),q = (x >> p) + 1;
					if(f[p][q] && a[now] < p || !f[p][q] && a[x] >= p)
						{_s -= now,ok = 0;break;}
					else _s -= x,st.pop();
				}if(ok)st.push(now);
				s[ans[W][i].first] = _s;
			}for(ll i = 1;i <= n;i++)if(s[i] == -1)s[i] = s[i - 1];
		}s[1] = 1;
	//	for(ll i = 1;i <= n;i++)cout << s[i] << " ";
	//	cout << "\n";
		for(ll i = 1;i <= m;i++)S ^= i * s[b[i]];
		cout << S << "\n";
	}return 0;
}
